package com.trkj.service.hjj;

import java.math.BigDecimal;
import java.rmi.server.ServerCloneException;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.trkj.dao.hjj.AttendancecardDAO;
import com.trkj.dao.hjj.TimecardDAO;
import com.trkj.dao.wdm.StaffInformationDAO;
import com.trkj.mybatisdao.TimesCardDAO;
import com.trkj.mybatisdao.staffDeptDAO;
import com.trkj.pojo.Attendancecard;
import com.trkj.pojo.StaffInformation;
import com.trkj.pojo.Timecard;
import com.trkj.service.ServiceException;
import com.trkj.vo.hjj.DeptVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class TimeCardModuleService {
	@Autowired
	private staffDeptDAO staffdeptDAO;//员工和部门
	
	@Autowired
	private AttendancecardDAO attendancecardDAO;//考勤配卡
	
	@Autowired
	private TimecardDAO timecardDAO;//考勤卡
	
	@Autowired
	private StaffInformationDAO staffInformationDAO;//员工
	
	@Autowired
	private TimesCardDAO timesCardDAO;//考勤卡
	/**
	 * 按照员工名字模糊搜索
	 * @param stainName
	 * @return
	 */
	public JSONArray findEmpMOHU(String stainName){
		return staffdeptDAO.findEmpMOHU("%"+stainName+"%");
	}
	/**
	 * 查询有员工的部门
	 * @param dName
	 * @return
	 */
	public JSONArray findDeptFindEmp(String dName){
		return staffdeptDAO.findDeptFindEmp(new DeptVo("'"+dName+"'"));
	}
	/**
	 * 查询所有的部门
	 * @return
	 */
	public JSONArray find_All_Dept(){
		return staffdeptDAO.find_All_Dept();
	}
	/**
	 * 考勤配卡
	 * @param stainId 员工id
	 * timecardId1 原来的考勤卡
	 * @param timecardId  新考勤卡id
	 * @param attcardId 考勤配卡id
	 * @param exchageTime 生效时间
	 * @return
	 */
	public boolean isExchangeTimecard(Long stainId,Long timecardId,Long timecardId1,Long attcardId,Timestamp exchageTime)throws ServiceException{
		try {
			
			//先找到考勤配卡
			Attendancecard att = attendancecardDAO.findOne(attcardId);
			//删除
			if(attendancecardDAO.exists(attcardId)){
				attendancecardDAO.delete(att);
			}
			//修改原来的考勤卡状态等
			Timecard timecard = timecardDAO.findOne(timecardId1);
			System.out.println(timecard+">");
			timecard.setStaffInformation(null);
			timecard.setTimecardBeizhu("无");
			//换卡员工
			StaffInformation staff = staffInformationDAO.findOne(stainId);
			//找到新的考勤卡
			Timecard timecardnew = timecardDAO.findOne(timecardId); 
			timecardnew.setTimecardBeizhu("有");
			timecardnew.setStaffInformation(staff);
			//新增考勤配卡
			Attendancecard attcard = new Attendancecard();
			attcard.setAttcardEffect(exchageTime);
			attcard.setAttcardLoseeff(new Timestamp(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS").parse("2099/12/31 23:59:59:111").getTime()));
			attcard.setAttcardRemarks("换卡");
			attcard.setAttcardStartdate(new Timestamp(new Date().getTime()));
			attcard.setStaffInformation(staff);
			attcard.setTimecard(timecardnew);
			attendancecardDAO.save(attcard);
			return true;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
		
	}
	/**
	 * 查询所有的员工考勤卡
	 * @return
	 */
	public JSONArray findTimeCardandEmp(){
		return timesCardDAO.findTimecardandEmp();
	}
	/**
	 * 打开考勤卡
	 */
	public boolean openTimecard(Long timecardId,Long timecardZt)throws ServiceException{
		try {
			Timecard timecard = timecardDAO.findOne(timecardId);
			timecard.setTimecardZt(new BigDecimal(timecardZt));
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 查询所有的停用的考勤卡
	 * @return
	 */
	public JSONArray findTimecardBystaeOne(){
		return timesCardDAO.findTimecardBystaeOne();
	}
	/**
	 * 查询未使用的考勤卡
	 * @param timecardId
	 * @return
	 */
	public JSONArray findTimeCardBytimecardId(Integer timecardId){
		return timesCardDAO.findTimecardBytimecardId(timecardId);
	}
	/**
	 * 配卡
	 * @param timecardId
	 * @param stainId
	 * @param timecardDate
	 * @return
	 */
	public boolean fenpeiTimecard(Long timecardId,Long stainId,Timestamp timecardDate,String sname)throws ServiceException{
		try {
			
			StaffInformation staff = staffInformationDAO.findOne(stainId);
			Timecard timecard = timecardDAO.findOne(timecardId);
			Attendancecard att = new Attendancecard();
			att.setStaffInformation(staff);
			att.setTimecard(timecard);
			att.setAttcardEffect(new Timestamp(new Date().getTime()));
			att.setAttcardLoseeff(new Timestamp(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS").parse("2099/12/31 23:59:59:111").getTime()));
			att.setAttcardPerson(sname);
			att.setAttcardStartdate(new Timestamp(new Date().getTime()));
			attendancecardDAO.save(att);
			
			timecard.setTimecardBeizhu("有");
			timecard.setStaffInformation(staff);
			timecard.setTimecardDate(timecardDate);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	/**
	 * 查询未分配的考勤卡
	 * @return
	 */
	public JSONArray findTimecardStateZero(){
		return timesCardDAO.findTimecard_statezero();
	}
	/**
	 * 删除考勤配卡
	 * @param attcardId
	 * @param timecardId
	 * @return
	 * @throws ServiceException
	 */
	public boolean delTimecardWhih(Long attcardId,Long timecardId)throws ServiceException{
		try {
			//删除该考勤配卡
			Attendancecard att = attendancecardDAO.findOne(attcardId);

			//找到该考勤卡
			Timecard time = timecardDAO.findOne(timecardId);
			time.setStaffInformation(null);
			time.setTimecardBeizhu("无");
			if(attendancecardDAO.exists(attcardId)){
				attendancecardDAO.delete(att);
			}
			
			
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
		
	}
	/**
	 * 查询考勤配卡根据考勤卡
	 * @param timecardId
	 * @return
	 */
	public JSONArray FindTimecardwhithBytimecardId(Integer timecardId){
		return timesCardDAO.findTimecardwhithBytimecardId(timecardId);
	}
	/**
	 * 查询所有的考勤配卡
	 * @return
	 */
	public JSONArray findAllAttendancecard(){
		return timesCardDAO.findAllAttendancecard();
	}
	/**
	 * 根据编号查询考勤卡
	 * @param timecardId
	 * @return
	 */
	public JSONArray findAlltiemCard(Integer timecardId){
		return timesCardDAO.findTimeCardByIds(timecardId);
	}
	/**
	 * 新增考勤配卡
	 * @param stainId
	 * @param timecardId
	 * @param attcardEffect
	 * @param attcardLoseeff
	 * @param attcardReason
	 * @param attcardRemarks
	 * @return
	 * @throws ServiceException
	 */
	public boolean inserAttendancecard(Long stainId,Long timecardId,Timestamp attcardEffect,Timestamp attcardLoseeff,String attcardReason,String attcardRemarks,Timestamp attcardStartdate,String StainName)throws ServiceException{
		try {
			//修改考勤卡状态及是否使用
			Timecard timecard=timecardDAO.findOne(timecardId);
			//查员工
			StaffInformation staff = staffInformationDAO.findOne(stainId);
			//修改考勤卡
			timecard.setTimecardBeizhu("有");
			timecard.setStaffInformation(staff);
			
			//新增考勤配卡
			Attendancecard attendancecard = new Attendancecard();
			attendancecard.setStaffInformation(staff);
			attendancecard.setTimecard(timecard);
			attendancecard.setAttcardEffect(attcardEffect);
			attendancecard.setAttcardLoseeff(attcardLoseeff);
			attendancecard.setAttcardReason(attcardReason);
			attendancecard.setAttcardRemarks(attcardRemarks);
			attendancecard.setAttcardStartdate(attcardStartdate);
			attendancecard.setAttcardPerson(StainName);
			attendancecardDAO.save(attendancecard);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 连表查所有的员工和部门
	 * @param page
	 * @param rows
	 * @return
	 */
	public JSONArray findAllStaffDept(){
		return staffdeptDAO.findAllStaffDept();
	}
	
	/**
	 * 查询所有的考勤卡
	 * @return
	 */
	public JSONArray findAllTimecardStaff(){
		return staffdeptDAO.findAllTimecardStaff();
	}
}
